package com.cmccgz.udacs_landlord_webapp.main.warning.presenters;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.Media;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.data.UpLoadFileRepository;
import com.cmccgz.udacs_landlord_webapp.data.UploadFileDataSource;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.WarningEditContract;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import rx.Subscriber;

public class WarningEditPresenter implements WarningEditContract.Presenter {
    private WarningEditContract.View view;
    private UploadFileDataSource dataSource;
    public WarningEditPresenter(WarningEditContract.View view) {
        this.view = view;
        dataSource=new UpLoadFileRepository();
    }

    @Override
    public void waringReject(final String w_id, String d_id, final String desc, List<String> urls) {
        if (StringUtils.isEmpty(w_id)) {
            view.warningRejectError("系统异常:w_id为空！");
            return;
        }
        if (StringUtils.isEmpty(d_id)) {
            view.warningRejectError("请选择设备！");
            return;
        }
        if (StringUtils.isEmpty(desc)) {
            view.warningRejectError("请填写描述！");
            return;
        }
        dataSource.upLoadFiles(urls, new UploadFileDataSource.UploadFileLisenter() {
            @Override
            public void uploadSuccess(List<String> medias) {
                //...否决
                List<Media>mediaList=new ArrayList<Media>();
                for (int i = 0; medias!=null&&i < medias.size(); i++) {
                    mediaList.add(new Media(medias.get(i)));
                }
                ApiService.rejectWarning(w_id,desc,mediaList).subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.warningRejectError("操作失败！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            view.warningRejectSuccess();
                        }else{
                            view.warningRejectError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
            }

            @Override
            public void uploadError(String msg) {
                view.warningRejectError(msg);
            }
        });
    }

    @Override
    public void waringEdit(final String w_id, String d_id, final String desc, List<String> urls) {
        if (StringUtils.isEmpty(d_id)) {
            view.warningEditError("请选择设备！");
            return;
        }
        if (StringUtils.isEmpty(desc)) {
            view.warningEditError("请填写描述！");
            return;
        }
        dataSource.upLoadFiles(urls, new UploadFileDataSource.UploadFileLisenter() {
            @Override
            public void uploadSuccess(List<String> medias) {
                List<Media>mediaList=new ArrayList<Media>();
                for (int i = 0; medias!=null&&i < medias.size(); i++) {
                    mediaList.add(new Media(medias.get(i)));
                }
                ApiService.editWarning(w_id,desc,mediaList).subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.warningEditError("操作失败！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            view.warningEditSuccess();
                        }else{
                            view.warningEditError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
            }

            @Override
            public void uploadError(String msg) {
                view.warningEditError(msg);
            }
        });
    }

    @Override
    public void waringAdd(final String d_id, final String desc, List<String> urls) {
        if (StringUtils.isEmpty(d_id)) {
            view.warningAddError("请选择设备！");
            return;
        }
        if (StringUtils.isEmpty(desc)) {
            view.warningAddError("请填写描述！");
            return;
        }
        dataSource.upLoadFiles(urls, new UploadFileDataSource.UploadFileLisenter() {
            @Override
            public void uploadSuccess(List<String> media_ids) {
                List<Media>medias=new ArrayList<Media>();
                for (int i = 0; media_ids!=null&&i < media_ids.size(); i++) {
                    medias.add(new Media(media_ids.get(i)));
                }
                ApiService.createWarning(new Date().getTime()+"", AppConfig.getMobile(GlobalContext.getInstance()),desc,d_id,medias).subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.warningAddError("报障失败，请稍后再试！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            view.warningAddSuccess();
                        }else{
                            view.warningAddError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
            }

            @Override
            public void uploadError(String msg) {
                view.warningAddError(msg);
            }
        });
    }

}
